//
//  PubuliuFrame.m
//  瀑布流
//
//  Created by 刘家俊 on 17/4/25.
//  Copyright © 2017年 Andrew. All rights reserved.
//

#import "PubuliuFrame.h"
#import "PubliuModel.h"

#define Margin 10
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height

@implementation PubuliuFrame

- (void)setModel:(PubliuModel *)model
{
    _model = model;
    
    /** 图片 */
    CGFloat left = 10;
    CGFloat right = 10;
    CGFloat colouMargin = 10;
    NSInteger columnCount = 2;
    CGFloat cellW = (SCREENWIDTH - left - right - (columnCount - 1) * colouMargin) / columnCount;
    
    NSString *height = [NSString stringWithFormat:@"%@",model.height];
    NSString *with = [NSString stringWithFormat:@"%@",model.width];
    
    CGFloat h = [height floatValue];
    CGFloat w = [with floatValue];
    CGFloat imageH = cellW * h / w;
    self.image = CGRectMake(0, 0, cellW, imageH);
    
    /** 标题 */
    CGFloat titleW = cellW - 2 * Margin;
    CGFloat titleY = CGRectGetMaxY(self.image) + Margin;
    CGSize titleSize = [self sizeWithText:model.title font:[UIFont systemFontOfSize:14] maxW:titleW];
    self.titleLabel = (CGRect){{Margin ,titleY} ,titleSize};
    
    /** 头像 */
    CGFloat photoWH = 20;
    CGFloat photoY = CGRectGetMaxY(self.titleLabel) + Margin;
    self.photo = CGRectMake(Margin, photoY, photoWH, photoWH);
    
    /** 点赞数 */
    CGFloat spotLabelH = 12;
    CGFloat spotLabelY = photoY + spotLabelH / 2 - 2;
    CGSize spotSize = [self sizeWithText:model.pv font:[UIFont systemFontOfSize:11] maxW:cellW];
    CGFloat spotLabelX = cellW - spotSize.width - Margin;
    self.spotLabel = CGRectMake(spotLabelX, spotLabelY, spotSize.width, spotLabelH);
    
    /** 点赞图标 */
    CGFloat spotImageWH = 12;
    CGFloat spotImageX = spotLabelX - Margin - spotImageWH + 8;
    self.spotImage = CGRectMake(spotImageX, spotLabelY, spotImageWH, spotImageWH);
    
    /** 评论数 */
    CGFloat commentLabelH = 12;
    CGSize commentLabelSize = [self sizeWithText:model.pv font:[UIFont systemFontOfSize:11] maxW:cellW];
    CGFloat commentLabelX = spotImageX - commentLabelSize.width - Margin + 2;
    self.commentLabel = CGRectMake(commentLabelX, spotLabelY, commentLabelSize.width, commentLabelH);
    
    /** 评论图标 */
    CGFloat commentImageWH = 12;
    CGFloat commentImageX = commentLabelX - commentImageWH - Margin + 8;
    self.commentImage = CGRectMake(commentImageX, spotLabelY, commentImageWH, commentImageWH);
    
    /** 名称 */
    CGFloat nameX = CGRectGetMaxX(self.photo) + Margin;
    CGFloat nameW = commentImageX - nameX - Margin;
    CGFloat nameH = 15;
    CGFloat nameY = photoY + nameH / 2 - 5;
    self.nameLabel = CGRectMake(nameX, nameY, nameW, nameH);
    
    /** originalview */
    CGFloat originalH = CGRectGetMaxY(self.photo) + Margin;
    self.originalView = CGRectMake(0, 0, cellW, originalH);
    
    /** cell的高度 */
    self.cellHeight = CGRectGetMaxY(self.originalView);

}

//label自适应高度
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *atts = [NSMutableDictionary dictionary];
    atts[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:atts context:nil].size;
}

@end
